<?php
class AjaxAction extends IntesysAction {
	
	
	public function testhtml(){
		$this -> display();
	}
	public function index(){
		//  通过mission往attendence表写入数据，（考勤）
		//@ 此处是将mission里面的所有考勤添加至attendence，实际是将刚刚通过的单条数据写入attendence
		exit('null');
		$sql=D('Mission')->where('particular != 0 and particular != 2')->select();//41条
		//var_dump($sql);
		foreach($sql as $k =>$v){
			$intable=D('Attendence')->where('member_id ='.$v['member_id'])->find();
			if($intable){
				$data=array();
				//修改
				$data['type']='update';
				if($v['particular']==1){
					$data['leavehour']=$intable['leavehour']+ceil(($v['etime']-$v['stime'])/3600);
					$data['leavecount']=$intable['leavecount']+1;
				}elseif($v['particular']==3){
					$data['neglect']=$intable['neglect']+1;
				}elseif($v['particular']==4){
					$data['madhour']=$intable['madhour']+ceil(($v['etime']-$v['stime'])/3600);
					$data['madcount']=$intable['madcount']+1;
				}
				$addin=D('Attendence')->where('id = '.$intable['id'])->data($data)->save();
			}else{
				//添加
				$data=array();
				$data['type']='add';
				$data['site_id']=$v['site_id'];
				$data['member_id']=$v['member_id'];
				$data['name']=D('Member')->where('id ='.$v['member_id'])->getfield('name',false);
				$data['position_id']=D('Member')->where('id ='.$v['member_id'])->getfield('memberpositionid',false);
				$posid=array_filter(explode(',',$data['position_id']));//数组
				if($posid){
					$position='';
					foreach($posid as $key =>$val){
						$posname=D('Member_position')->where('id = '.$val)->getfield('name',false);
						$position=$position.$posname.'/';
					}
					$position=substr($position,0,-1);
				}else{
					$position='';
				}
				$data['position_name']=$position;
				if($v['particular']==1){
					$data['leavehour']=ceil(($v['etime']-$v['stime'])/3600);
					$data['leavecount']=1;
				}elseif($v['particular']==3){
					$data['neglect']=1;
				}elseif($v['particular']==4){
					$data['madhour']=ceil(($v['etime']-$v['stime'])/3600);
					$data['madcount']=1;
				}
				$addin=D('Attendence')->data($data)->add();
			}
		}
	}
	public function newindex(){
		$sql=D('Mission')->where('particular != 0 and particular != 2')->field('id,site_id,member_id,createtime,proposername,particular,stime,etime')->select();//41条
		foreach($sql as $key =>$val){
			foreach($val as $k =>$v){
				$data[$k]=$val[$k];
				$data['mission_id']=$val['id'];
				unset($data['id']);
				$data['name']=$val['proposername'];
				$data['starttime']=$val['stime'];
				$data['endtime']=$val['etime'];
			}
			$sql=D('Attendence')->data($data)->add();
		}
	}
	public function mission(){
		Vendor('Pinyin.pinyin');
		$py=new cn2pinyin();

		$sql=D('Mission')->select();
		foreach($sql as $k =>$v){
			$data['id']=$v['id'];
			$data['memberstring']=$py->get($v['proposername']);
			//将申请人的姓名拼音写入数据库
			$insert=D('Mission')->data($data)->save();
		}
	}
	public function sqlin(){
		//用于同步积分事件表和积分排名表
		$re=D('Integral_log')->where('site_id = 322')->select();
		foreach($re as $key =>$v){
			$logid=$v['id'];
			$data['num']=$v['changenum'];
			$data['total']=$v['totalnum'];
			D('Integralranking')->where('integrallogid ='.$logid)->save($data);
		}
	}
	public function randsql(){
		$re=D('Integral_log')->where('site_id = 322 ')->order('id')->select();
		foreach($re as $k =>$v){
			$sum=D('Integral_log')->where('id <= '.$v['id'].' and member_id ='.$v['member_id'].' and site_id = 322')->sum('changenum');
			D('Integral_log')->where('id ='.$v['id'])->setfield('totalnum',$sum);
		}
	}
	public function sqltoinfo(){
		$re=D('Info')->where('site_id >= 322')->select();
		foreach($re as $k =>$v){
			$memberpositionname=D('Member')->where('id ='.$v['member_id'])->getField('memberpositionname');
			D('Info')->where('member_id ='.$v['member_id'])->setfield('memberpositionname',$memberpositionname);
		}
	}
}